//
//  TopUpHistoryTableViewCell.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/16.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "TopUpHistoryTableViewCell.h"

@interface TopUpHistoryTableViewCell ()

Strong UIImageView *titleImageView;
Strong UILabel *titleLabel;
Strong UILabel *timeLabel;
Strong UIButton *deleteButton; /**<*/

@end

@implementation TopUpHistoryTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    self = [super initWithCellIdentifier:cellID];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _titleImageView = [UIImageView new];
        _titleImageView.contentMode = UIViewContentModeScaleAspectFit;
        _titleImageView.image = [UIImage imageNamed:@"TopUpHistory_left"];
        
        _titleLabel  = [[UILabel alloc]init];
        _titleLabel.font = DEF_FontSize_14;
        _titleLabel.textColor = getColor(textColor);
        _titleLabel.numberOfLines = 2;
        
        _timeLabel  = [[UILabel alloc]init];
        _timeLabel.font = DEF_FontSize_14;
        _timeLabel.textColor = getColor(mainColor);
        
        _deleteButton = [[UIButton alloc]init];
        _deleteButton.titleLabel.font = DEF_FontSize_13;
        _deleteButton.layer.borderColor = getColor(mainColor).CGColor;
        _deleteButton.layer.borderWidth = 1;
        _deleteButton.layer.masksToBounds = YES;
        _deleteButton.layer.cornerRadius = 5;
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView sd_addSubviews:@[_titleLabel,_titleImageView,_deleteButton,_timeLabel]];
        
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(57 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(27 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(190 / WIDTH_5S_SCALE);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo( - 10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(20 / WIDTH_5S_SCALE);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.timeLabel.mas_bottom).offset(0);
        make.right.mas_equalTo(- 10 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(32 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(15 / WIDTH_5S_SCALE);
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.titleLabel.attributedText = [self getAttributeName:[NSString stringWithFormat:@"%@",model[@"balance"]] info:@"成功"];
    self.timeLabel.text = [self getStringFromDateString:[NSString stringWithFormat:@"%@",model[@"createTime"]]];
}

- (NSAttributedString *)getAttributeName:(NSString *)name info:(NSString *)info{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:name attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"充值金额:"]];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"充值状态:"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:info]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

//时间戳转换为时间
- (NSString *)getStringFromDateString:(NSString *)dateString{
    NSString *timeStampString  = dateString;
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dString       = [formatter stringFromDate: date];
    return dString;
}

#pragma mark - click

- (void)deleteButtonClick{
    if (_getDeleteClick) {
        _getDeleteClick();
    }
}

@end
